package lang

import (
	"HandWritingJVM/native"
	"HandWritingJVM/rtda"
	"HandWritingJVM/rtda/heap"
	"fmt"
)

func init() {
	fmt.Println("loading class native method - getPrimitiveClass")
	native.Register("java/lang/Class", "getPrimitiveClass", "(Ljava/lang/String;)Ljava/lang/Class;", getPrimitiveClass)
	fmt.Println("loading class native method - getName0")
	native.Register("java/lang/Class", "getName0", "()Ljava/lang/String;", getName0)
	fmt.Println("loading class native method - desiredAssertionsStatus0")
	native.Register("java/lang/Class", "desiredAssertionsStatus0", "(Ljava/lang/Class;)Z", desiredAssertionsStatus0)
}
//本地方法 getPrimitiveClass 具体实现
func getPrimitiveClass(frame *rtda.Frame) {
	nameObj := frame.LocalVars().GetRef(0)
	name := heap.GoString(nameObj)
	loader := frame.Method().Class().Loader()
	class := loader.LoadClass(name).JClass()
	frame.OperandStack().PushRef(class)
}

//本地方法 getName0 具体实现
func getName0(frame *rtda.Frame) {
	this := frame.LocalVars().GetThis()
	class := this.Extra().(*heap.Class)
	name := class.JavaName()
	nameObj := heap.JString(class.Loader(), name)
	frame.OperandStack().PushRef(nameObj)
}

//本地方法 desiredAssertionsStatus0 具体实现
func desiredAssertionsStatus0(frame *rtda.Frame) {
	frame.OperandStack().PushBoolean(false)
}
